09. String Index
Indexing
Did you know that you can access individual characters in a string? To access an individual character, you can use the character's location in the string, called its index. Just put the index of the character inside square brackets (starting with [0]
as the first character) immediately after the string. For example:
"James"[0];
Returns: "J"
or more commonly, you will see it like this, using a variable:
var name = "James";
name[0];
Returns: "J"
String Index
INSTRUCTOR NOTE:
Characters within a string are indexed starting from 0, where the first character is at position 0, to n-1, where the last character is at position n-1 (n represents the total number of characters within a string).
Characters in a String